<?php

namespace maotool\view\html;

use DOMDocument;
use DOMNode;
use maotool\object\StrUtil;

/**
 * @property $innerText
 * @property $tagName
 */
class Text
{

    /**
     * @var DOMNode
     */
    private $node;

    /**
     * @param DOMNode $node
     */
    public function __construct(&$node = null)
    {
        if (!is_null($node)) {
            $this->node = $node;
        }
    }

    public function __get($name)
    {
        switch ($name) {
            case 'tagName':
                return "#TEXT";
            case 'innerHTML':
            case 'outerHTML':
            case 'innerText':
                return strip_tags($this->node->ownerDocument->saveHtml($this->node));
            case 'children':
            case 'attributes':
            case 'childNodes':
                return [];
        }
        return $this->$name;
    }

    public function __set($name, $value)
    {
        switch ($name) {
            case 'outerHTML':
                $children = [];
                $parentNode = $this->node->parentNode;
                foreach ($parentNode->childNodes as $childNode) {
                    if ($this->node->isSameNode($childNode)) {
                        $document = new DOMDocument('1.0', 'UTF-8');
                        $document->formatOutput = false;
                        $document->preserveWhiteSpace = false;
                        $id = uniqid();
                        @$document->loadHTML('<div id="com-maotool-html-' . $id . '">' . $value . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL);
                        $docNode = $document->getElementById("com-maotool-html-" . $id);
                        if (isset($docNode)) {
                            foreach ($docNode->childNodes as $docChildNode) {
                                $children[] = $this->node->ownerDocument->importNode($docChildNode, true);
                            }
                        }
                    } else {
                        $children[] = $childNode->cloneNode(true);
                    }
                }
                while ($parentNode->hasChildNodes()) {
                    $parentNode->childNodes[0]->parentNode->removeChild($parentNode->childNodes[0]);
                }
                foreach ($children as $child) {
                    $parentNode->appendChild($child);
                }
                break;
            case 'innerHTML':
                while ($this->node->hasChildNodes()) {
                    $this->node->childNodes[0]->parentNode->removeChild($this->node->childNodes[0]);
                }
                $document = new DOMDocument('1.0', 'UTF-8');
                $document->formatOutput = true;
                $document->preserveWhiteSpace = true;
                $id = uniqid();
                @$document->loadHTML('<div id="com-maotool-html-' . $id . '">' . $value . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL);
                $docNode = $document->getElementById("com-maotool-html-" . $id);
                if (isset($docNode)) {
                    foreach ($docNode->childNodes as $childNode) {
                        $this->node->appendChild($this->node->ownerDocument->importNode($childNode, true));
                    }
                }
                break;
            case 'innerText':
                $this->node->nodeValue = $value;
                break;
            default:
                $this->$name = $value;
                break;
        }
    }

}